//
//  HRHomeTableViewController.swift
//  microBlog
//
//  Created by HR_W on 16/5/19.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit

import SVProgressHUD

let homeCellID = "homeCellID"


class HRHomeTableViewController: HRBaseTableViewController {

    let refreshView = HRRefreshControl()
    
    lazy var statusListViewModel: HRStatusListViewModel = HRStatusListViewModel()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !userlogin {
            //用户未登录 设置访客视图的信息
            visitorLoginView.updateUI("关注一些人,你将获得意想不到的惊喜,快来注册吧!", imageName: "visitordiscover_feed_image_smallicon",isHome: true)
            
            return
        }
      
        
        setNavBar()
        
        prepareForTableView()
        
        loadData()
        
       
    }
    //设置导航条
    private func setNavBar() {
    
        let popItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(HRHomeTableViewController.push))
        
        navigationItem.rightBarButtonItem = popItem
    
    }
    
   @objc private func push() {
    let detail = HRDetailViewController()
    
    detail.hidesBottomBarWhenPushed = true
    navigationController?.pushViewController(detail, animated: true)
    
    }
    
    private lazy var pullupView: UIActivityIndicatorView = {
        let indicatorView = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
    
        return indicatorView
    }()
    
    private func prepareForTableView(){
    
        tableView.registerClass(HRStatusCell.self, forCellReuseIdentifier: homeCellID)
        //tableView.rowHeight = 250
        tableView.rowHeight = UITableViewAutomaticDimension
        
        tableView.estimatedRowHeight = 300
        //设置分割线
        tableView.separatorStyle = .None
       // refreshControl = UIRefreshControl()
        
        
        
        refreshView.addTarget(self, action: #selector(HRHomeTableViewController.loadData), forControlEvents: .ValueChanged)
        self.tableView.addSubview(refreshView)
//        refreshControl?.addTarget(self, action: #selector(HRHomeTableViewController.loadData), forControlEvents: .ValueChanged)
         tableView.tableFooterView = pullupView
    }

}
//加载数据
extension HRHomeTableViewController{
    
    @objc private func loadData() {
    
    statusListViewModel.loadData(pullupView.isAnimating()) { (isSuccess) in
            self.pullupView.stopAnimating()
            //self.refreshControl?.endRefreshing()
        self.refreshView.endRefreshing()
            if !isSuccess {
            
            SVProgressHUD.showErrorWithStatus(AppErrorTip)
                return
            }
            self.tableView.reloadData()
        }
    
    }

}

extension HRHomeTableViewController {

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.statusViewModelArray.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(homeCellID, forIndexPath: indexPath) as! HRStatusCell
        if indexPath.row == statusListViewModel.statusViewModelArray.count - 1 && !pullupView.isAnimating() {
            pullupView.startAnimating()
            
            loadData()
        }
        
        cell.statusViewModel = statusListViewModel.statusViewModelArray[indexPath.row]
    
       
        return cell
    }

}